Thursday, September 3, 2020

STM32F103R6 Common Anode Seven Segments Display And Switch Interfacing

In previous post I showed a simple 7-Segment interfacing with the STM32F103R6. Now I add a push button to make a pulse counting. It counts up to 0x0F and then it rolls down to 0.

STM32F103R6 Common Anode Seven Segments Display And Switch Interfacing

Simulating Program Using Proteus VSM 8.15

 Using HAL driver allow us to configure its peripherals without writing the configuration codes.

STM32F103R6 Common Anode Seven Segments Display And Switch Interfacing
STM32F103R6 Pins Configuration
Only PortC is required. Between PC0 and PC7 are digital output that drive a single common anode 7-Segment display. PC2 is an digital input connects to a momentary switch. It's active low as its default high state is already configure in software. 

  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file : main.c
  5.   * @brief : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   * opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23. /* Private function prototypes -----------------------------------------------*/
  24. void SystemClock_Config(void);
  25. static void MX_GPIO_Init(void);
  26. /* USER CODE BEGIN PFP */
  27.  
  28. const unsigned char dAnode[16] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,
  29. 0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
  30. char temp=0,gpioNum=0x01,cnt=0;
  31.  
  32. int main(void)
  33. {
  34. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  35. HAL_Init();
  36.  
  37. /* Configure the system clock */
  38. SystemClock_Config();
  39.  
  40. /* Initialize all configured peripherals */
  41. MX_GPIO_Init();
  42.  
  43. while (1)
  44. {
  45. gpioNum=0x01;
  46. temp=0x01;
  47. for(int i=0;i<8;i++){
  48. HAL_GPIO_WritePin(GPIOC,gpioNum,dAnode[cnt]&temp);
  49. gpioNum<<=1;
  50. temp<<=1;
  51. }
  52.  
  53. if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12)==GPIO_PIN_RESET){
  54. cnt++;
  55. HAL_Delay(250);
  56. }
  57. if(cnt>15) cnt=0;
  58. }
  59. /* USER CODE END 3 */
  60. }
  61.  
  62. /**
  63.   * @brief System Clock Configuration
  64.   * @retval None
  65.   */
  66. void SystemClock_Config(void)
  67. {
  68. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  69. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  70.  
  71. /** Initializes the RCC Oscillators according to the specified parameters
  72.   * in the RCC_OscInitTypeDef structure.
  73.   */
  74. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  75. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  76. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  77. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  78. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  79. {
  80. Error_Handler();
  81. }
  82. /** Initializes the CPU, AHB and APB buses clocks
  83.   */
  84. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  85. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  86. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  87. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  88. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  89. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  90.  
  91. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  92. {
  93. Error_Handler();
  94. }
  95. }
  96.  
  97. /**
  98.   * @brief GPIO Initialization Function
  99.   * @param None
  100.   * @retval None
  101.   */
  102. static void MX_GPIO_Init(void)
  103. {
  104. GPIO_InitTypeDef GPIO_InitStruct = {0};
  105.  
  106. /* GPIO Ports Clock Enable */
  107. __HAL_RCC_GPIOC_CLK_ENABLE();
  108. __HAL_RCC_GPIOA_CLK_ENABLE();
  109.  
  110. /*Configure GPIO pin Output Level */
  111. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  112. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  113.  
  114. /*Configure GPIO pins : PC0 PC1 PC2 PC3
  115.   PC4 PC5 PC6 PC7 */
  116. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  117. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  118. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  119. GPIO_InitStruct.Pull = GPIO_NOPULL;
  120. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  121. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  122.  
  123. /*Configure GPIO pin : PC12 */
  124. GPIO_InitStruct.Pin = GPIO_PIN_12;
  125. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  126. GPIO_InitStruct.Pull = GPIO_PULLUP;
  127. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  128.  
  129. }
  130.  
  131. /* USER CODE BEGIN 4 */
  132.  
  133. /* USER CODE END 4 */
  134.  
  135. /**
  136.   * @brief This function is executed in case of error occurrence.
  137.   * @retval None
  138.   */
  139. void Error_Handler(void)
  140. {
  141. /* USER CODE BEGIN Error_Handler_Debug */
  142. /* User can add his own implementation to report the HAL error return state */
  143. __disable_irq();
  144. while (1)
  145. {
  146. }
  147. /* USER CODE END Error_Handler_Debug */
  148. }
  149.  
  150. #ifdef USE_FULL_ASSERT
  151. /**
  152.   * @brief Reports the name of the source file and the source line number
  153.   * where the assert_param error has occurred.
  154.   * @param file: pointer to the source file name
  155.   * @param line: assert_param error line source number
  156.   * @retval None
  157.   */
  158. void assert_failed(uint8_t *file, uint32_t line)
  159. {
  160. /* USER CODE BEGIN 6 */
  161. /* User can add his own implementation to report the file name and line number,
  162.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  163. /* USER CODE END 6 */
  164. }
  165. #endif /* USE_FULL_ASSERT */
  166.  
  167. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  168.  

 

Click here to download its source file.  

 For other similar posts please check,

  1. Getting Started With STM32F103C8T6 Module with STM32CubeIDE
  2.  STM32F103C8T6 Blue Pill SysTick and Multiplexing Display Example
  3.  STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick
  4.  STM32F103C8T6 Blue Pill SysTick LED Blinking
  5. STM32F103R6 Common Anode Seven Segments Display Example 
  6. STM32F103R6 Common Anode Seven Segments Display And Switch Interfacing 
  7. STM32F103R6 Simple 2-Digit Multiplexing Display And Switch Example 

No comments:

Post a Comment